package de.hijacksoft.isledesktopsync;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SettingsManager {

    private static final String FILENAME = "settings.conf";
    
    private String file = "";
    private static SettingsManager instance = null;

    private SettingsManager(){
        super();
        try {
            if(!new File(FILENAME).exists()) {
                new File(FILENAME).createNewFile();
                System.out.println("settings initialized");
            }
            BufferedReader in = new BufferedReader(new FileReader(FILENAME));
            String str;
            while ((str = in.readLine()) != null) {
                file += str + "\n";
            }
            in.close();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }
        instance = this;
    }
    
    public static SettingsManager getInstance(){
        return instance == null ? new SettingsManager() : instance;
    }

    /**
     * This method returns a Sstring value for the given key
     * @param key the key of the setting e.g. WindowMode
     */
    public String getString(String key){
        String line = getLine(key);
        if(line != null){
            line = line.replaceAll(key + "=", "");
            return line;
        }
        else return null;
    }
    
    /**
     * This method returns a boolean value for the given key
     * @param key the key of the setting e.g. WindowMode
     */
    public boolean getBoolean(String key){
        return Boolean.parseBoolean(getString(key));
    }
    
    /**
     * This method returns an int value for the given key
     * @param key the key of the setting e.g. WindowMode
     */
    public int getInt(String key){
        return Integer.parseInt(getString(key));
    }

    
    private String getLine(String key){
        // regex pattern and matcher
        Pattern p = Pattern.compile(key + "=" + ".+");
        Matcher m = p.matcher(file);
        
        if(m.find()){ // key found
            return m.group();
        }
        else return null;
    }
    
    public void saveString(String key, String value){
        boolean exists = false;

        // regex pattern and matcher
        Pattern p = Pattern.compile(key + "=");
        Matcher m = p.matcher(file);
        
        if(m.find()){ // key found
            file = file.replaceAll(key + "=" + ".+", key + "=" + value);
        }
        else {        // key not found        
            file += key + "=" + value;
        }
        
        updateFile();
    }
    
    public void saveBoolean(String key, boolean value){
        saveString(key, value + "");
    }
 
    public void saveInt(String key, int value){
        saveString(key, value + "");
    }
        
    private void updateFile(){        
        // delete old file
        new File(FILENAME).delete();
        
        // write to new file
        FileWriter fstream;
        try {
            fstream = new FileWriter(FILENAME);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(file);
            out.close();
        } catch (IOException ex) {
            System.out.println(ex.toString());
        }

    }
    
}